﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DataProvider;

namespace SGBDSecurity
{
    public partial class InsertRow : Form
    {

        UserDataProvider aUserDataProvider;
        User aUser;
        Table aTable;
        TableData aTableData;

        public InsertRow(User iUser, Table iTable)
        {
            aTable = iTable;
            aUser = iUser;
            aUserDataProvider = new UserDataProvider();
            InitializeComponent();
            aTableData = aUserDataProvider.GetViewTable(aUser, aTable);
            this.lNome.Text = iUser.Login;
            this.lNivel.Text = iUser.Classe + "";

            foreach (string temp in aTableData.Colunas)
            {
                DataGridViewColumn column = new DataGridViewColumn();
                column.HeaderText = temp;
                column.CellTemplate = new DataGridViewTextBoxCell();
           
                dataGridView1.Columns.Add(column);
            }
            dataGridView1.Rows.Add();

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                this.lblMessage.Text = "";
                string valores = "";
                string temp = "";
                for (int i = 0; i < this.aTableData.Colunas.Count; i++)
                {
                    if (aTable.STipoSeguranca.Equals("campo") && i == ((i / 2) * 2) && dataGridView1.Rows[0].Cells[i].Value != null && dataGridView1.Rows[0].Cells[i + 1].Value == null)
                    {

                        PopUpSecurityLevel aPop = new PopUpSecurityLevel(aTableData.Colunas[i + 1]);
                        aPop.Visible = true;
                        return;

                    }
                    if (aTableData.TipoColuna[i] == "System.String")
                    {

                        if (dataGridView1.Rows[0].Cells[i].Value != null)
                        {
                            valores += "'" + dataGridView1.Rows[0].Cells[i].Value.ToString() + "'";

                        }
                        else valores += "NULL";
                    }
                    else if (aTableData.TipoColuna[i] == "System.Int32")
                    {
                        if (dataGridView1.Rows[0].Cells[i].Value != null)
                            temp = dataGridView1.Rows[0].Cells[i].Value.ToString();
                        else temp = "NULL";
                        try
                        {
                            int aTempInt = -1;
                            if (!temp.Equals("NULL")) aTempInt = int.Parse(temp);
                            valores += temp;
                            if ((aTable.STipoSeguranca.Equals("campo") && i != ((i / 2) * 2) && aTempInt != -1) || (aTable.STipoSeguranca.Equals("registro") && i == this.aTableData.Colunas.Count - 1))
                            {
                                if (aTempInt < aUser.Classe || aTempInt > 4)
                                {
                                    PopUpSecurityLevel aPopUp = new PopUpSecurityLevel(aTableData.Colunas[i]);
                                    aPopUp.Visible = true;
                                    dataGridView1.Rows[0].Cells[i].Value = string.Empty;
                                    return;
                                }
                            }
                        }
                        catch (Exception g)
                        {
                            TypeExcpetion aJanelaException = new TypeExcpetion(aTableData.Colunas[i], aTableData.TipoColuna[i]);
                            aJanelaException.Visible = true;
                            dataGridView1.Rows[0].Cells[i].Value = string.Empty;
                            return;
                        }

                    }
                    else if (aTableData.TipoColuna[i] == "System.Decimal")
                    {
                        if (dataGridView1.Rows[0].Cells[i].Value != null)
                            temp = dataGridView1.Rows[0].Cells[i].Value.ToString();
                        else temp = "NULL";
                        try
                        {
                            if (!temp.Equals("NULL"))
                            {
                                decimal.Parse(temp);
                                valores += temp;
                            }
                            else valores += "NULL";

                        }
                        catch (Exception h)
                        {
                            TypeExcpetion aJanelaException = new TypeExcpetion(aTableData.Colunas[i], aTableData.TipoColuna[i]);
                            aJanelaException.Visible = true;
                            dataGridView1.Rows[0].Cells[i].Value = string.Empty;
                            return;
                        }
                    }
                    else
                    {
                        valores += "NULL";
                    }
                    if (i < this.aTableData.Colunas.Count - 1)
                    {
                        valores += ",";
                    }
                }
                aUserDataProvider.InsertTable(aTable, valores);
                dataGridView1.Rows.RemoveAt(0);
                dataGridView1.Rows.Add();
                PopUp pop = new PopUp("Row Inserted!");
                pop.Visible = true;
            }
            catch (Exception ex)
            {
                this.lblMessage.Text = ex.Message;
            }
            
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
